import Vue from 'vue'
import Vuex from 'vuex'
import managerApi from "../api/managerApi";
import modelsApi from "../api/modelsApi";
import util from "../util/util";
import ws from "../socket"
Vue.use(Vuex)
const socket = new ws({
    url:'ws://127.0.0.1:1699/ws',
    reconnectTimes:0
})
export default new Vuex.Store({
    state: {
        uploadAction: "/api/upload/file",
        userInfo: "",
        models:{},
        menuList:"",
        authList:"",
        pathList:"",
        websocket:socket,
    },
    mutations: {
        setPathList(state,data){
            state.pathList=data
        },
        setAuthList(state,data){
            state.authList=data
        },
        setMenuList(state, data){
          state.menuList=data
        },
        setUserInfo(state, data) {
            state.userInfo = data
        },
        setModels(state,data){
            state.models=data
        },

    },
    actions: {
        defendMenuList(context){
          if(context.state.menuList == ""||context.state.authList==""){
                modelsApi.getMenu().then(res=>{
                    context.state.menuList=res.data.menu
                    let ret=[]
                    let path=[]
                    res.data.opt.forEach(item=>{
                        if(item.path!=""){
                            path.push(item.path)
                        }
                        ret.push(item.permission)
                    })
                    context.state.authList=ret.toString()
                    res.data.menu.forEach(item=>{
                        path.push(item.path)
                    })
                    context.state.pathList=path.toString()
                    window.sessionStorage.setItem("pathList",util.encrypt(path.toString()))
                })
          }
        },
        defendUserInfor(context) {
            if (context.state.userInfo == "") {
                managerApi.getInfo().then(res => {
                    context.commit("setUserInfo", res.data)
                })
            }
        }
    },
    modules: {}
})
